package com.ngocvo.QuanLyThuChi;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class ThongKeActivity extends Activity {    
    //Các hằng dùng cho tạo Option Menu
   // private static final int DELETE_WORK = Menu.FIRST;
   // private static final int ABOUT = Menu.FIRST + 2;
	private static final int HOME = Menu.FIRST;
    
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, HOME, 0,"Home" ).setIcon(android.R.drawable.ic_menu_info_details);
		return true;
	}
	
	public boolean onOptionsItemSelected(android.view.MenuItem item) {
		switch (item.getItemId()) {
		case HOME:
			Intent i = new Intent(ThongKeActivity.this, StartActivity.class);
			startActivity(i);
			break;
		}
		return true;
	}
	
    ArrayList<CashFlow> array;
    ListCashFlowAdapter arrayAdapter;
    Intent i;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thongke_layout);
        
        final TextView tv_Title = (TextView)findViewById(R.id.lbl_thongke);
        final ListView list = (ListView) findViewById(R.id.cashList);
        
        array = new ArrayList<CashFlow>();
        CashFlowData CFData = new CashFlowData(ThongKeActivity.this);
        
        Bundle b = this.getIntent().getExtras();
        final int flowtype = b.getInt("flowType");
        String strTitle = b.getString("title"); 
        tv_Title.setText(strTitle);
        
        try{
            ArrayList<CashFlow> cashList = CFData.getAllByFlowType(flowtype);
            array = cashList;            
            arrayAdapter = new ListCashFlowAdapter(this, 
                    R.layout.list, array);
            
            //Tạo list view cho danh sách công việc                
            list.setAdapter(arrayAdapter);
            
        }catch (Exception e) {
			AlertDialog.Builder builder = new AlertDialog.Builder(ThongKeActivity.this);
			builder.setTitle("Error orrcurs!");
			builder.setMessage("The error is: " + e.getMessage());
			builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub								
				}
			});
			builder.show();
		}

        list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				TextView tv_id = (TextView)view.findViewById(R.id.cash_id);				
				Bundle b = new Bundle();
				b.putString("cash_id", tv_id.getText().toString());
				
				switch (flowtype) {
				case 0:
					i = new Intent(ThongKeActivity.this, ThuUpdateActivity.class);
					i.putExtras(b);
					startActivity(i);					
					break;
				case 1:
					i = new Intent(ThongKeActivity.this, ChiUpdateActivity.class);
					i.putExtras(b);
					startActivity(i);					
					break;
				case 2:
					i = new Intent(ThongKeActivity.this, VayUpdateActivity.class);
					i.putExtras(b);
					startActivity(i);					
					break;
				case 3:
					i = new Intent(ThongKeActivity.this, NoUpdateActivity.class);
					i.putExtras(b);
					startActivity(i);
				default:
					break;
				}
			}
		});
    }
    
    
    
    
    
    //Tạo Option Menu
  /*  public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);        
        menu.add(0, DELETE_WORK, 0,"Delete" ).setIcon(android.R.drawable.ic_delete);        
        menu.add(0, ABOUT, 0,"About" ).setIcon(android.R.drawable.ic_menu_info_details);
        return true;
    }
    
    //Xử lý sự kiện khi các option trong Option Menu được lựa chọn
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {            
            case DELETE_WORK: {
                deleteCheckedWork();
                break;
            }            
            case ABOUT: {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("VietAndroid");
                builder.setMessage("AUTHOR:" + "\n" + "  Nguyen Anh Tuan" + "\n" + "SOURCE:" + "\n" + "  diendan.vietandroid.com");
                builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {                                    
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                builder.setIcon(android.R.drawable.ic_dialog_info);
                builder.show();
                break;
            }
        }
        return true;
       }

    private void deleteCheckedWork() {
        if (array.size() > 0) {
            for (int i = array.size() - 1; i >= 0; i--) {
                if (array.get(i).isChecked()) {
                    array.remove(i);
                    arrayAdapter.notifyDataSetChanged();
                    continue;
                }
            }
        }        
    }*/		
}
